package Dao;

import Utils.JDBCUtils;
import com.alibaba.druid.util.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao {

    private QueryRunner queryRunner = new QueryRunner();
    //增删改通用方法
    public static  int  aduMthod(Connection conn,String sql,Object...args){
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                pst.setObject(i+1,args[i]);
            }
         return  pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        JDBCUtils.closeResource(conn,pst);
        }
        return 0;
    }
    //单个查询通用方法
    public static <T> T queryOne(Class<T> clazz,Connection conn,String sql,Object...args){
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                pst.setObject(i+1,args[i]);
            }
            rs = pst.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if(rs.next()){
                T t = clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object values = rs.getObject(i + 1);
                    String Name = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(Name);
                    field.setAccessible(true);
                    field.set(t,values);
                }
                return  t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
        JDBCUtils.CloseResource(conn,pst,rs);
        }
        return null;
    }
    //查询一系列通用方法
    public static <T> List<T>queryForlist(Class<T> clazz,Connection conn, String sql,Object...args){
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                pst.setObject(i+1,args[i]);
            }
            rs = pst.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            ArrayList<T> list = new ArrayList<>();
            while(rs.next()){
                T t = clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object values = rs.getObject(i + 1);
                    String Name = rsmd.getColumnLabel(i+1);
                    Field field = clazz.getDeclaredField(Name);
                    field.setAccessible(true);
                    field.set(t,values);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
        JDBCUtils.CloseResource(conn,pst,rs);
        }
        return null;
    }


    public Object queryForSingleValue(Connection conn, String sql, Object... args){
        try {
            return queryRunner.query(conn, sql, new ScalarHandler(), args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn);
        }
        return null;

    }





}
